package com.example.hello.controller;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.hello.entity.EmpDO;
import com.example.hello.entity.ExprDO;
import com.example.hello.model.EmpDetailVO;
import com.example.hello.model.EmpPageRequest;
import com.example.hello.model.PageResult;
import com.example.hello.model.Result;
import com.example.hello.model.request.LoginRequest;
import com.example.hello.model.response.LoginResponse;
import com.example.hello.service.EmpService;
import com.example.hello.util.JwtTokenUtil;

@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 用户登录接口
     * 
     * @param loginRequest 登录请求
     * @return 登录响应
     */
    @PostMapping("/login")
    public Result<LoginResponse.LoginResponseData> login(@RequestBody LoginRequest loginRequest) {
        // 根据用户名查询员工信息
        EmpDO emp = empService.getEmpByUsername(loginRequest.getUsername());

        // 如果员工不存在或密码不匹配，返回登录失败
        if (emp == null || !loginRequest.getPassword().equals(emp.getPassword())) {
            return Result.fail(400, "用户名或密码错误");
        }

        // 登录成功，生成token
        String token = jwtTokenUtil.generateToken(emp.getUsername(), Long.valueOf(emp.getId()));


        // 构建响应数据
        LoginResponse.LoginResponseData responseData = LoginResponse.LoginResponseData.builder()
                .token(token)
                .empDO(emp)
                .build();

        // 返回成功响应
        return Result.success("登录成功", responseData);
    }

    // 分页查询员工
    @GetMapping("/getAll")
    public Result<PageResult<EmpDO>> getEmpPage(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String gender,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        EmpPageRequest request = new EmpPageRequest();
        request.setName(name);
        request.setGender(gender);
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setPageNum(page);
        request.setPageSize(pageSize);

        return Result.success(empService.getEmpPage(request));
    }

    // 根据id查询员工详情
    @GetMapping("/{id}")
    public Result<EmpDetailVO> getEmpDetail(@PathVariable Integer id) {
        return Result.success(empService.getEmpDetail(id));
    }

    // 新增员工
    @PostMapping
    public Result<Void> createEmp(@RequestBody EmpDO emp) {
        empService.createEmp(emp);
        return Result.success(null);
    }

    // 更新员工
    @PutMapping("/{id}")
    public Result<Void> updateEmp(@PathVariable Integer id, @RequestBody EmpDO emp) {
        emp.setId(id);
        empService.updateEmp(emp);
        return Result.success(null);
    }

    // 删除员工
    @DeleteMapping("/{id}")
    public Result<Void> deleteEmp(@PathVariable Integer id) {
        empService.deleteEmp(id);
        return Result.success(null);
    }

    // 批量删除员工
    @DeleteMapping("/batch")
    public Result<Void> deleteEmps(@RequestParam @Value("ids") List<Integer> ids) {
        empService.deleteEmps(ids);
        return Result.success(null);
    }

    // 新增员工工作经历
    @PostMapping("/{empId}/exprs")
    public Result<Void> addExpr(@PathVariable Integer empId, @RequestBody ExprDO expr) {
        empService.addExpr(empId, expr);
        return Result.success(null);
    }

    // 删除员工工作经历
    @DeleteMapping("/exprs/{exprId}")
    public Result<Void> deleteExpr(@PathVariable Integer exprId) {
        empService.deleteExpr(exprId);
        return Result.success(null);
    }
}